package edu.pku.sei.sct.util;

import edu.pku.sei.ocl.evaluation.Context;
import edu.pku.sei.ocl.evaluation.OCLVariable;

public class ContextFrameWrapper {
	private Context context;
	public ContextFrameWrapper(Context c) {
		context = c;
	}
	
	private Integer hashCode = null;
	
	public int hashCode() {
		if(hashCode==null) {
			int i = 0;
			for(OCLVariable o : context.getKey2value().keySet())
				i+=o.hashCode();
			hashCode = i;
		}
		
		return hashCode;
		
	}
	
	public boolean equals(Object o) {
		if(o instanceof Context) {
			return context == o;
		} else if(o instanceof ContextFrameWrapper) {
			ContextFrameWrapper oc = (ContextFrameWrapper)o;
			
			if(context.getKey2value().size()!=oc.context.getKey2value().size()) 
				return false;
			
			for(OCLVariable v : context.getKey2value().keySet()) {
				Object v1 = context.getValue(v);
				Object v2 = oc.context.getValue(v);
				
				if((v1==null&&v2==null)||(v1!=null&&v2!=null))
					continue;
				else return false;
			}
			return true;
		}
		return false;
	}
}
